package com.zhongli.devplatform.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.regexp.internal.RE;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.dto.ChooseGradeInfoDTO;
import com.zhongli.devplatform.po.sys.ReadChapterExercisesPO;
import com.zhongli.devplatform.po.sys.ReadChapterPO;
import com.zhongli.devplatform.po.sys.ReadKnowledgePointPO;
import com.zhongli.devplatform.po.user.ReadUserPO;
import com.zhongli.devplatform.po.view.ViewUserToSchoolPO;
import com.zhongli.devplatform.service.imp.sys.ReadChapterExercisesService;
import com.zhongli.devplatform.service.imp.sys.ReadChapterService;
import com.zhongli.devplatform.service.imp.sys.ReadKnowledgePointService;
import com.zhongli.devplatform.service.imp.user.ReadUserService;
import com.zhongli.devplatform.service.imp.view.ViewUserToSchoolPOService;
import com.zhongli.devplatform.utils.BeanCopierUtil;
import com.zhongli.devplatform.vo.sys.ChapterAndKnowledgePointVO;
import com.zhongli.devplatform.vo.sys.ChapterExercisesVO;
import com.zhongli.devplatform.vo.sys.ReadKnowledgePointVO;
import com.zhongli.devplatform.bean.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wy
 * @since 2020/7/31
 */
@Api(tags = "章节", description = "章节，章节首页，操作")
@RestController
@RequestMapping("/chapter")
public class ChapterController {

    @Autowired
    private ReadChapterService chapterService;

    @Autowired
    private ReadKnowledgePointService knowledgePointService;

    @Autowired
    private ReadChapterExercisesService chapterExercisesService;

    @Autowired
    private ReadUserService userService;

    @Resource
    private ViewUserToSchoolPOService userToSchoolPOService;


    @ApiOperation("根据当前学生获取所有的章节信息")
    @GetMapping("/getCurrentStuChapter")
    public Res getCurrentStuChapter(){
        Integer currentStu = SecurityHelper.getCurrentUserId();
        List<ReadChapterPO> data = chapterService.getCurrentStuChapter(currentStu);
        return Res.ok(data);
    }


    @ApiOperation("根据科目id 获取学生当前年级所有的章节信息")
    @GetMapping("/getCurrentStuGradeChapter/{subjectId}")
    public Res<List<ReadChapterPO>> getCurrentStuGradeChapter(@PathVariable Integer subjectId){
        Integer currentStu = SecurityHelper.getCurrentUserId();
        ViewUserToSchoolPO viewUserToSchoolPO = userToSchoolPOService.getById(currentStu);
        if (viewUserToSchoolPO == null) return Res.ok(new ArrayList<>());
        Integer gradeId = viewUserToSchoolPO.getGradeId();
        List<ReadChapterPO> data = chapterService.list(new QueryWrapper<ReadChapterPO>()
                .eq("subject_id", subjectId)
                .eq("grade_id", gradeId));
        return Res.ok(data);
    }



    /**
     *根据当前学生 选择的年级 年级id gradeId  上下册 0,1 volumeLevel 科目id subjectId
     * @param gradeInfo {@link ChooseGradeInfoDTO}
     * @return 结果集
     */
    @ApiOperation("根据当前学生 选择的年级 年级id gradeId  上下册 0,1 volumeLevel 科目id subjectId  学习报告")
    @GetMapping("/getCurrentStuChooseGradeInfo")
    public Res<List<ChapterAndKnowledgePointVO>> getCurrentStuChooseGradeInfo(@ModelAttribute ChooseGradeInfoDTO gradeInfo){
        List<ReadChapterPO> data = chapterService.list(new QueryWrapper<ReadChapterPO>()
                .eq("grade_id",gradeInfo.getGradeId())
                .eq("subject_id",gradeInfo.getSubjectId()).orderByAsc("chapter_title"));
        if (data.isEmpty()) return Res.ok(new ArrayList<>());
        List<ChapterAndKnowledgePointVO> chapterAndKnowledgePointVO = BeanCopierUtil.copy(data, ChapterAndKnowledgePointVO.class);
        assert chapterAndKnowledgePointVO != null;
        chapterAndKnowledgePointVO.forEach(item->{
            List<ReadKnowledgePointPO> knowledgePoints = knowledgePointService.list(new QueryWrapper<ReadKnowledgePointPO>()
                            .eq("chapter_id",item.getId()).orderByAsc("knowledge_name"));
            List<ReadKnowledgePointVO> result = BeanCopierUtil.copy(knowledgePoints, ReadKnowledgePointVO.class);
            AtomicInteger schedule = new AtomicInteger();
            result.forEach(KnowledgePointVO  -> {
                ReadChapterExercisesPO exercisesPO = chapterExercisesService.getOne(new QueryWrapper<ReadChapterExercisesPO>()
                        .eq("student_id", SecurityHelper.getCurrentUserId())
                        .isNotNull("end_time")
                        .eq("knowledge_id", KnowledgePointVO.getId()));
                boolean fished = !(exercisesPO == null);
                if(fished) schedule.getAndIncrement();
                KnowledgePointVO.setIsFinish(fished);
            });
            item.setKnowledgePoints(result);
            item.setSchedule(schedule.get());
        });
        return Res.ok(chapterAndKnowledgePointVO);
    }
}
